﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Article;
using Enterprise.Service.Article;
using Enterprise.Model.OfficeDocument;
using Enterprise.Service.OfficeDocument;
using Enterprise.Service.Department;
using Enterprise.Service.User;
using Enterprise.Control;
using Enterprise.Service.Module;
namespace Enterprise.UI.Web.Modules.Article
{
    public partial class Edit :Enterprise.Service.BasePage
    {
        protected int Id = (int)Utility.sink("Id", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string Cmd = (string)Utility.sink("Cmd", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected int ModuleId = (int)Utility.sink("ModuleId", Utility.MethodType.Get, 0, 0, Utility.DataType.Int);
        string odId = (string)Utility.sink("odId", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string s = ModuleService.GetModuleName(ModuleId);
                lb_MName.Text = s;
                OnStart();
            }
        }

        /// <summary>
        /// 初始化加载项
        /// </summary>
        private void OnStart()
        {
            int deptId = 0;
            lb_User.Text = UserService.GetUserName(Utility.Get_UserId, out deptId);
            lb_Dept.Text = DepartmentService.GetDepartMentName(deptId);
            if (Cmd == "Edit")
            {
                OnBindData();
            }
            else if (Cmd == "Delete")
            {
                ArticleService aService = new ArticleService();
                var q = aService.ArticleDisp(Id);
                if (q != null)
                {
                    string _note = "操作成功";
                    q.DB_Option_Action = "Delete";
                    try
                    {
                        aService.ArticleExecute(this.Page,q);
                    }
                    catch (Exception ex)
                    {
                        _note = ex.Message;
                    }
                    Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx?ModuleId=" + ModuleId);
                }
            }
            else
            {
                CreateBT.AddButton("back.gif", "返回", "Index.aspx?ModuleId=" + ModuleId, Utility.PopedomType.List, HeadMenu1);
            }
            if (!string.IsNullOrEmpty(odId))
            {
                OfficeDocService odService = new OfficeDocService();
                var query = odService.OfficeDocumentDisp(odId);
                if (query != null)
                {
                    t_Title.Text = query.ODCNAME;
                    t_TitleRu.Text = query.ODRNAME;
                    t_Content.Text = query.ODCREMARK;
                    t_ContentRu.Text = query.ODRREMARK;
                }
            }
        }

        /// <summary>
        /// 当编辑时绑定数据项
        /// </summary>
        private void OnBindData()
        {
            ArticleService aService = new ArticleService();
            var q = aService.ArticleDisp(Id);
            if (q != null)
            {                
                ListItem li = t_Type.Items.FindByValue(q.AType);
                if (li != null)
                {
                    li.Selected = true;
                }
                t_Time.Text = q.AInvalidTime.ToShortDateString();
                t_Title.Text = q.ATitle;
                t_TitleRu.Text = q.ATitleRu;
                t_Content.Text = q.AContent;
                t_ContentRu.Text = q.AContentRu;
                rcv_users_value.Value = q.ARecevieUser;
            }            
        }

        /// <summary>
        /// 保存操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                ArticleService aService = new ArticleService();
                ArticleEntity entity=new ArticleEntity();
                if (string.IsNullOrEmpty(Cmd))
                {
                    entity.DB_Option_Action = "Insert";
                    entity.AReleaseTime = DateTime.Now;
                    entity.ACreateUser = Utility.Get_UserId;
                    entity.ACreateTime = DateTime.Now;
                    entity.AModuleId = ModuleId;
                    entity.ADepartMent = UserService.GetUserDeptId(Utility.Get_UserId);
                    entity.AUser = Utility.Get_UserId;
                }
                else if (Cmd == "Edit")
                {                    
                    entity = aService.ArticleDisp(Id);
                    ModuleId = entity.AModuleId;
                    entity.DB_Option_Action = "Update";
                }
                entity.AType = (string)Utility.sink(t_Type.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.AInvalidTime = (DateTime)Utility.sink(t_Time.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Date);
                entity.ATitle = (string)Utility.sink(t_Title.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.ATitleRu = (string)Utility.sink(t_TitleRu.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.AContent = (string)Utility.sink(t_Content.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);
                entity.AContentRu = (string)Utility.sink(t_ContentRu.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str);

                //签收范围格式@1,4,5
                string rcv_users = rcv_users_value.Value;
                rcv_users = rcv_users.TrimEnd(',');
                entity.ARecevieUser = rcv_users;//签收范围                          
                aService.ArticleExecute(this.Page,entity);
                if (entity.DB_Option_Action == "Insert")
                {
                    Enterprise.Service.Rtx.RtxService.SendRtxMessageServices(entity.ARecevieUser, "办公事务 - " + ModuleService.GetModuleName(ModuleId), "发布新内容：" + entity.ATitle,false);
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "Index.aspx?ModuleId=" + ModuleId);
        }

    }
}